/*
Search JSObject
for(i in row) {
        alert(i);
    }
* */

/*
* Send FormData to Json
* */

/*Submit FormTableData to Struts2 action Method Insert to table.*/
function SubmitForm(formName) {
    var form = $get(formName);
    var Mform = new mini.Form(formName);

    Mform.validate();
    if (Mform.isValid() == false) return;



    var param = GetFormParamter(formName);
    var action = form.getAttribute("action");
    var request = new $AjaxRequest(baseUrl+action, param, function(result){
        getJsonResult(result,eval(form.getAttribute("onSuccess")));

    });
    request.Start();
}
/*Get DataGrid PageData*/
function GridData_Page(gridName, pageName, dataurl, e, paramCustom) {
    var grid = mini.get(gridName);
    var page = mini.get(pageName);
    var param = "";
    if(e instanceof Object) {
        param = "page="+true+"&pageIndex="+(e.pageIndex)*e.pageSize+"&pageSize="+e.pageSize;
    }else {
        param = "page="+true+"&pageIndex="+0+"&pageSize="+10;
    }
    param += paramCustom;
    //alert(param);

    var request = new $AjaxRequest(baseUrl+dataurl, param, function(result){
       getJsonResult(result, function(){
           grid.setData(result.data);
           page.setTotalCount(result.total);
       });
    });
    request.Start();
}


/*Get JSONData Msg Success or Err*/
function getJsonResult(result, onSuccess) {
    //call back Method
    if(result.code) {
        switch(parseInt(result.code)) {
            case 0:
            case 1:
                 onSuccess(result);
            break;
            case -1:
            case -2:
                mini.open({
                    url: "/psw/common/jsp/test.jsp",
                    title: "Message", width: 380, height:200,
                    allowResize: false,
                    onload: function () {
                        var iframe = this.getIFrameEl();
                        iframe.contentWindow.SetResult(result);
                    },
                    ondestroy: function () {

                    }
                });
            break;
        }
    }else {
        if(onSuccess) {
            onSuccess(result);
        }
    }

}

/*Get the page input data.*/
function GetFormParamter(formName)
{
    var element;
    var name;
    var form = $get(formName);
    var param = new StringBuilder();


    var elements = form.getElementsByTagName("input");
    var length = elements.length;


    for (var i = 0; i < length; i++)
    {
        element = elements[i];
        name = element.name;
        if (typeof (name) === "undefined" || (name === null) || (name.length === 0) || element.disabled)
        {
            continue;
        }
        var type = element.type;
        if ((type === 'text') || (type === 'password') || (type === 'hidden') || (((type === 'checkbox') || (type === 'radio')) && element.checked))
        {
            param.append(encodeURIComponent(name));
            param.append("=");
            param.append(encodeURIComponent(element.value));
            param.append("&");
        }

    }
    elements = form.getElementsByTagName("textarea");
    length = elements.length;
    for (var i = 0; i < length; i++)
    {
        element = elements[i];
        name = element.name;
        if (typeof (name) === "undefined" || (name === null) || (name.length === 0) || element.disabled)
        {
            continue;
        }

        param.append(encodeURIComponent(name));
        param.append("=");
        param.append(encodeURIComponent(element.value));
        param.append("&");

    }
    elements = form.getElementsByTagName("select");
    length = elements.length;
    for (var i = 0; i < length; i++)
    {
        element = elements[i];
        name = element.name;
        var isFind = false;
        for (var j = 0; j < form.length; j++)
        {
            if (form.elements[j].name == name)
            {
                isFind = true;
                break;
            }
        }
        if (!isFind)
        {
            alert(name);
        }
        if (typeof (name) === "undefined" || (name === null) || (name.length === 0) || element.disabled)
        {
            continue;
        }
        var optionCount = element.options.length;
        for (var j = 0; j < optionCount; j++)
        {
            var option = element.options[j];
            if (option.selected)
            {
                param.append(encodeURIComponent(name));
                param.append("=");
                param.append(encodeURIComponent(option.value));
                param.append("&");
            }
        }
    }
    return param.toString();
}

